package com.vertxsample.samples.core.db

import com.zaxxer.hikari.HikariConfig
import com.zaxxer.hikari.HikariDataSource
import org.jooq.DSLContext
import org.jooq.impl.DSL
import java.sql.Connection
import java.sql.SQLException


class HikariPoolUtil {



    companion object {

        // 获取连接池
        fun getDataSource( config:HikariConfig): HikariDataSource? {
            var dataSource: HikariDataSource? = null
            try {
                dataSource = HikariDataSource(config)
            } catch (e: Exception) {
                e.printStackTrace()
            }

            return dataSource
        }
        // 获取连接
        fun getConnection(dataSource: HikariDataSource?): Connection? {
            return if (dataSource != null) {
                try {
                    dataSource!!.getConnection()
                } catch (e: Exception) {
                    null
                }

            } else {
                null
            }
        }

        // 关闭连接池
        fun closeDataSource(dataSource: HikariDataSource) {
            dataSource.close()
        }

        // 关闭连接
        @Throws(SQLException::class)
        fun closeConnection(con: Connection) {
            con.close()
        }

        //获取数据库连接
        fun getContext(conDsl: Connection): DSLContext {
            return DSL.using(conDsl)
        }
    }
}